package com.huadao.cart_ccs.experiment.paramsSetting;

import com.huadao.cart_ccs.Event.ChangeAirEvent;
import com.huadao.cart_ccs.common.ParamsPreferences;
import com.huadao.cart_ccs.data.bo.CO2BO;
import com.huadao.cart_ccs.data.bo.CommandWord;
import com.huadao.cart_ccs.data.bo.O2BO;
import com.huadao.cart_ccs.data.bo.TemperatureBO;
import com.huadao.cart_ccs.modle.CO2Modle;
import com.huadao.cart_ccs.modle.O2Modle;
import com.huadao.cart_ccs.modle.TemperatureModle;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class SetParamsPresenter extends SetParamsContract.ISetParamsPresenter {

    private CO2Modle co2Modle;
    private O2Modle o2Modle;
    private TemperatureModle temperatureModle;
    private boolean isSetCO2Succ;
    private boolean isSetO2Succ;
    private boolean isSetTemperatureSucc;

    public SetParamsPresenter(SetParamsContract.ISetParamsView baseView) {
        this.baseView = baseView;
        attachView(baseView);
        co2Modle = new CO2Modle();
        o2Modle = new O2Modle();
        temperatureModle = new TemperatureModle();
        EventBus.getDefault().register(this);
    }

    @Override
    double getDefaultO2() {
        return ParamsPreferences.getInstance().getO2();
    }

    @Override
    double getDefaultCO2() {
        return ParamsPreferences.getInstance().getCO2();
    }

    @Override
    double getDefaultTemperature() {
        return ParamsPreferences.getInstance().getTemperature();
    }

    @Override
    void saveDeviceParams() {
        ParamsPreferences.getInstance().setFluidIntakePump(baseView.getInputSpeed(),baseView.getInputDirection());
        ParamsPreferences.getInstance().setCirculatingPump(baseView.getCirclatingSpeed(),baseView.getCirclatingDirection());
        ParamsPreferences.getInstance().setWastePump(baseView.getWasteSpeed(),baseView.getWasteDirection());
        ParamsPreferences.getInstance().setReclaimPump(baseView.getReclaimSpeed(),baseView.getReclaimDirection());
        ParamsPreferences.getInstance().setStirrerSpeed(baseView.getStrrierSpeed());
        ParamsPreferences.getInstance().setCultureTankSpeed(baseView.getCultureSpeed());
        ParamsPreferences.getInstance().setShakeTableTime(baseView.getShakeTableTime());
        boolean isChangeAirChanged=ParamsPreferences.getInstance().getChangeAirAmount()!=baseView.getChangeAirAmount()||ParamsPreferences.getInstance().getChangeAirFrequency()!=baseView.getChangeAirFrequency();
        if(isChangeAirChanged){
            ParamsPreferences.getInstance().setChangeAirFrequency(baseView.getChangeAirFrequency()*60);
            ParamsPreferences.getInstance().setChangeAirAmount(baseView.getChangeAirAmount());
        }
        ParamsPreferences.getInstance().commit();
        if(isChangeAirChanged) {
            EventBus.getDefault().post(new ChangeAirEvent(baseView.getChangeAirAmount(), baseView.getChangeAirFrequency() * 60));
        }
        baseView.onSaveDeviceParamsSucc();
    }

    @Override
    void setEnvironment() {
//        if (ParamsPreferences.getInstance().getCO2() != baseView.getCO2()) {
//            isSetCO2Succ=false;
//            setCO2(baseView.getCO2());
//        } else {
//            isSetCO2Succ = true;
//        }
//        if (ParamsPreferences.getInstance().getO2()!= baseView.getO2()) {
//            isSetO2Succ=false;
//            setO2(baseView.getO2());
//        } else {
//            isSetO2Succ = true;
//        }
//        if (ParamsPreferences.getInstance().getTemperature() != baseView.getTemperature()) {
//            isSetTemperatureSucc=false;
//            setTemperature(baseView.getTemperature());
//        }else {
//            isSetTemperatureSucc=true;
//        }
//        if(isAllSucc()){
//            baseView.onSetEnvironmentSucc();
//        }

        isSetCO2Succ=false;
        isSetO2Succ=false;
        isSetTemperatureSucc=false;
        setCO2(baseView.getCO2());
        setO2(baseView.getO2());
        setTemperature(baseView.getTemperature());
    }

    void setCO2(double CO2) {
        co2Modle.setCO2(CO2);
    }

    void setO2(double O2) {
        o2Modle.setO2(O2);
    }

    void setTemperature(double temperature) {
        temperatureModle.setTemperature(temperature);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(CO2BO event) {
        if (event.getCommondWord() == CommandWord.writeSucc[0]) {
            baseView.onSetCO2Succ(event);
            isSetCO2Succ=true;
//            ParamsPreferences.getInstance().setCO2Commit(event.getCO2());
            if(isAllSucc()){
                baseView.onSetEnvironmentSucc();
            }
        } else if (event.getCommondWord() == CommandWord.writeFail[0]) {
            baseView.onSetCO2Failed(event);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(O2BO event) {
        if (event.getCommondWord() == CommandWord.writeSucc[0]) {
            baseView.onSetO2Succ(event);
            isSetO2Succ=true;
//            ParamsPreferences.getInstance().setO2Commit(event.getO2());
            if(isAllSucc()){
                baseView.onSetEnvironmentSucc();
            }
        } else if (event.getCommondWord() == CommandWord.writeFail[0]) {
            baseView.onSetO2Failed(event);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(TemperatureBO event) {
        if (event.getCommondWord() == CommandWord.writeSucc[0]) {
            baseView.onSetTemperatureSucc(event);
            isSetTemperatureSucc=true;
//            ParamsPreferences.getInstance().setTemperatureCommit(event.getTemperature());
            if(isAllSucc()){
                baseView.onSetEnvironmentSucc();
            }
        } else if (event.getCommondWord() == CommandWord.writeFail[0]) {
            baseView.onSetTemperatureFailed(event);
        }
    }

    private synchronized boolean isAllSucc(){
        return isSetCO2Succ&&isSetO2Succ&&isSetTemperatureSucc;
    }

    @Override
    public void detachView() {
        EventBus.getDefault().unregister(this);
        super.detachView();
    }
}
